import React, {Component} from 'react';

import './index.less';

import {
    base,
    high
} from 'nc-lightapp-front';

import AdjustSort from '../AdjustSort';

import HROrgReger from '../../../../../hrpub/common/components/referSearch/org';

import language from '../../language';
import setQueryMeta from "../../request-functions/setQueryMeta";
import getSysConf from '../../container/functions/getSysConf';

const {
    Refer
} = high;

const {
    NCCheckbox
} = base;

class Header extends Component {
    constructor(props) {
        super(props);

        this.adjustSort = new AdjustSort(props);

        this.handleHumanOrgTreeChange = this.handleHumanOrgTreeChange.bind(this);
        this.buttonClickHandle = this.buttonClickHandle.bind(this);
        this.onChangeHandle = this.onChangeHandle.bind(this);
    }

    handleHumanOrgTreeChange(value) {
        const {
            updateState,
            editTable,
            setButtonStatus2
        } = this.props;
        updateState({
            humanResourceOrganizationValue: value
        }, async () => {
            await this.getSysConf(value);
            if (this.props.hasSearched) {
                this.props.getMainTableData();
            }
            if (value) {
                // editTable.setTableData('ncc60071701list', {rows: []});
                setButtonStatus2('hasMainOrg');
            }
        });
    }

    getSysConf = (orgValue) => {
        return getSysConf.call(this, orgValue);
    };

    buttonClickHandle(props, btnCode) {
        const {page} = this.props;

        // 新增
        if (btnCode === 'Add') {
            this.props.toEditPage('add');
        }
        // 查询
        else if (btnCode === 'Query') {
            this.props.updateState({
                searchModalVisible: true
            }, () => {
                setQueryMeta.call(this);
                this.props.search.openAdvSearch('ncc60071701query', true);
            });
        }
        // 刷新
        else if (btnCode === 'Refresh') {
            this.props.getMainTableData();
        }
        // 排序
        else if (btnCode === 'Sort') {
            this.props.updateState({
                sortModalVisible: true
            });
        }
        // 批量删除
        else if (btnCode === 'BatchDelete') {
            this.props.deleteMainTableRow();
        }
        // 调整顺序
        else if (btnCode === 'AdjustSort') {
            this.adjustSort.showAdjustPage();
        }
        // 调整顺序取消
        else if (btnCode === 'Cancel' && page === 'adjust-sort') {
            this.adjustSort.closeAdjustPage();
        }
        // 调整顺序保存
        else if (btnCode === 'Save' && page === 'adjust-sort') {
            this.adjustSort.saveShowOrder();
        }
        // 转档
        else if (btnCode === 'SwitchToDoc') {
            this.props.updateState({
                switchToDocModalVisible: true
            });
        }
        // 批量修改
        else if (btnCode === 'BatchEdit') {
            this.props.getRef('batchEdit').getBatchTemplateData();
        }
        // 批量添加子集
        else if (btnCode === 'BatchAddSub') {
            this.props.batchAddSub();
        }
        // 花名册
        else if (btnCode === 'ListReport') {
            this.props.updateState({
                rosterModalVisible: true
            });
        }
        // 导出照片
        else if (btnCode === 'ExportPhoto') {
            this.props.exportPhoto();
        }
        // 任职历史
        else if (btnCode === 'JobHis') {
            this.props.showHisJobModal();
        }
        // 汇报对象
        else if (btnCode === 'ReptObj') {
            this.props.showReportTo();
        }
        // 生成用户
        else if (btnCode === 'psndocCreateUser') {
            this.props.createUser();
        }
        // 倒入照片
        else if (btnCode === 'ImportPhoto') {
            this.props.importPhotos();
        }
        // 打印
        else if (btnCode === 'Print') {
            this.props.print();
        }
        // 附件管理
        else if (btnCode === 'File') {
            this.props.fileManager()
        }
        // 输出
        else if (btnCode === 'output') {
            this.props.printOutput();
        } else if (btnCode === 'ExportPsnInfo') {
            this.props.exportPsn('cur');
        } else if (btnCode === 'ExportPsnInfoAll') {
            this.props.exportPsn('all');
        }
        // 联查卡片
        else if (btnCode === 'cardrpt') {
            this.props.previewCard();
        }

        // 导入  不用不设置即可
        else if (btnCode === 'ImportData') {
            this.props.updateState({
                importModalVisible: true
            })
        }
    }

    onChangeHandle(type) {
        return (value) => {
            this.props.updateState({
                [type]: value
            }, () => {
                if (this.props.hasSearched) {
                    this.props.getMainTableData();
                }
            });
        }
    }

    render() {
        const {
            humanResourceOrganizationValue,
            button,
            page,
            show_on_job_psn,
            include_child_orgs,
            include_child_depts,
            meta,
            subsetCheckboxDisabled,
            showSubsetChecked
        } = this.props;

        let tmp = meta.getMeta() || true;

        if (typeof tmp === 'object' && Object.keys(tmp).length <= 0) {
            tmp = false;
        }

        return (
            <div className="header">
                {
                    page === 'adjust-sort' &&
                    <div
                        onClick={() => {
                            this.adjustSort.closeAdjustPage();
                        }}
                        style={{
                            cursor: 'pointer'
                        }}
                    >
                        <i class="iconfont wui-left_bold"></i>{language['hi6007-000012']}{/* 国际化处理： 返回*/}
                    </div>
                }
                {
                    page === 'main' &&
                    <div className="header-refer">
                        <If condition={tmp}>
                            <HROrgReger
                                getOrgData={this.handleHumanOrgTreeChange}
                                orgVal={humanResourceOrganizationValue}
                            />
                        </If>
                    </div>
                }
                {
                    page === 'main' && humanResourceOrganizationValue.refpk &&
                    <div className="header-checkbox-group">

                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('show_on_job_psn')}
                            checked={show_on_job_psn}
                        >
                            {language['hi6007-000044']}{/* 国际化处理： 在职人员*/}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('include_child_orgs')}
                            checked={include_child_orgs}
                        >
                            {language['hi6007-000045']}{/* 国际化处理： 包含下级组织人员*/}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('include_child_depts')}
                            checked={include_child_depts}
                        >
                            {language['hi6007-000046']}{/* 国际化处理： 包含下级部门人员*/}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={(status) => {
                                this.props.updateState({
                                    subsetVisible: status,
                                    showSubsetChecked: status,
                                    isShowBottomDom: status
                                }, () => {
                                    setTimeout(() => {
                                        if (status) {
                                            this.props.insRef.subSetCard.initSubSet();
                                        }
                                    }, 1);
                                });
                            }}
                            disabled={subsetCheckboxDisabled}
                            checked={showSubsetChecked}
                        >
                            {language['hi6007-000094']}{/* 国际化处理： 显示子集*/}
                        </NCCheckbox>
                    </div>
                }

                {
                    humanResourceOrganizationValue.refpk &&
                    <div className="btn-group">
                        <div className="definition-icons">
                            {button.createButtonApp({
                                area: 'head',
                                onButtonClick: this.buttonClickHandle
                            })}
                        </div>
                    </div>
                }

            </div>
        );
    }
}

export default Header;
